package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.kaishengit.entity.Project;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class ProjectDao {
	private DBHelp<Project> db = new DBHelp<Project>();
	public Project findByProjectId(int projectid){
		String sql="SELECT id,projectname,projectdescription,manager,projectnumbers FROM t_project WHERE id=?";
		return db.executeQueryForObject(sql, new ProjectRowMapper(), projectid);
	} 
	public void insertNewProject(Project p){
		String sql="INSERT t_project(id,projectname,projectdescription,manager,projectnumbers) VALUES(?,?,?,?,?);";
		db.executeSQL(sql, p.getId(),p.getProjectName(),p.getProjectDescription(),p.getManager(),p.getProjectNumbers());
	}
	public Project findByProjectName(String projectname){
		String sql="SELECT id,projectname,projectdescription,manager,projectnumbers FROM t_project WHERE projectname=?";
		return db.executeQueryForObject(sql, new ProjectRowMapper(), projectname);
	}
	private class ProjectRowMapper implements RowMapper<Project>{

		public Project mapperRow(ResultSet rs) throws SQLException {
			// TODO Auto-generated method stub
			Project p = new Project();
			p.setId(rs.getInt("id"));
			p.setManager(rs.getString("manager"));
			p.setProjectDescription(rs.getString("projectDescription"));
			p.setProjectName(rs.getString("projectName"));
			p.setProjectNumbers(rs.getString("projectNumbers"));
			return p;
		}
		
	}
}
